package com.learn.xueer.ui.fragment

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.learn.xueer.app.base.BaseFragment1
import com.learn.xueer.app.ext.init
import com.learn.xueer.databinding.FragmentHearBinding
import com.learn.xueer.ui.activity.BookInfoActivity
import com.learn.xueer.ui.adapter.HearBookAdapter
import com.learn.xueer.ui.viewmodel.RequestHearViewModel
import kotlinx.android.synthetic.main.fragment_hear.recyclerView
import kotlinx.android.synthetic.main.fragment_hear.smartRefresh
import kotlinx.android.synthetic.main.fragment_home.recyclerViewMenu
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.parseState

class HearFragment : BaseFragment1<RequestHearViewModel, FragmentHearBinding>() {

    private var gradeId: Int? = null
    private var params = HashMap<String, Any>()
    private var page = 1
    private var pageSize = 15
    private val adapter: HearBookAdapter by lazy { HearBookAdapter(arrayListOf()) }
    override fun initView(savedInstanceState: Bundle?) {
        addLoadingObserve(mViewModel)
        arguments?.let {
            gradeId = it.getInt("gradeId", -1)
        }

        val layoutManager = StaggeredGridLayoutManager(3, RecyclerView.VERTICAL)
        recyclerView.init(layoutManager, adapter, false)
        smartRefresh.setOnRefreshListener {
            page = 1
            loadData()
        }

        smartRefresh.setOnLoadMoreListener {
            page++
            loadData()
        }

        adapter.setOnItemClickListener { adapter, view, position ->
           val intent = Intent(mActivity,BookInfoActivity::class.java)
            val bundle = Bundle()
            bundle.putParcelable("bookInfo",this.adapter.data[position])
            intent.putExtras(bundle)
            startActivity(intent)
        }
    }

    override fun lazyLoadData() {
        loadData()
    }

    private fun loadData() {
        params["page"] = page
        params["pageSize"] = pageSize
        if (gradeId != null && gradeId!! > 0) {
            params["grade"] = gradeId!!
        }
        mViewModel.hearSayReq(params)
    }

    override fun createObserver() {
        mViewModel.hearSayResult.observe(this, Observer { resultState ->
            parseState(resultState, {
                if (!smartRefresh.isRefreshing && page == 1) {
                    adapter.setList(it)
                }
                if (smartRefresh.isRefreshing) {
                    smartRefresh.finishRefresh()
                    adapter.setNewInstance(it)
                }
                if (smartRefresh.isLoading) {
                    smartRefresh.finishLoadMore()
                    adapter.addData(it!!)
                }
            }, {})
        })
    }

    companion object {
        fun newInstance(gradeId: Int): HearFragment {
            return HearFragment().apply {
                arguments = Bundle().apply {
                    putInt("gradeId", gradeId)
                }
            }
        }
    }
}